{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "######################################################################\n",
    "#\n",
    "# 遗传算法主体部分\n",
    "#\n",
    "######################################################################\n",
    "import time\n",
    "import numpy as np "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "### 寻找使得下面函数输出最大的X，Y\n",
    "def func(X,Y):\n",
    "  return -(X+3)**2-(Y-3)**2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "### time_start: 程序运行的开始时间\n",
    "### dim: 自变量的维度\n",
    "### iterate_times: 迭代次数\n",
    "### individual_n: 种群个体数量\n",
    "### p_c: 交叉的概率\n",
    "### p_m: 突变的概率\n",
    "### std_var: 突变的范围\n",
    "### num_range: 变量的范围\n",
    "time_start=time.time()\n",
    "dim = 2\n",
    "iterate_times = 150\n",
    "individual_n = 200\n",
    "p_c = 0.4\n",
    "p_m = 0.15\n",
    "std_var = 20\n",
    "num_range = [-100,100]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "### 初始化种群\n",
    "def initialize(individual_n,num_range):\n",
    "  return np.random.uniform(num_range[0],num_range[1],size = [individual_n,dim]) "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "### 交叉算子\n",
    "def overlapping(a,b,pc,dim):\n",
    "  T = np.random.choice(a=2, size=dim,p=[1-pc,pc])\n",
    "  for i in range(dim):\n",
    "    if T[i] == 1:\n",
    "      a[i],b[i] = b[i],a[i]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "### 变异算子\n",
    "def variation(a,pm,dim,num_range):\n",
    "  ### 随机从0，1中选择一个组成dim维的数组\n",
    "  T = np.random.choice(a=2, size=dim,p=[1-pm,pm]) \n",
    "  for i in range(dim):\n",
    "    ### 按照随机产生的T来确定是否变异\n",
    "    if T[i] == 1:\n",
    "      ### clip回型函数，使得数组在规定范围内\n",
    "      a[i] = np.clip(np.random.normal(loc = a[i], scale=std_var),num_range[0],num_range[1])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "### softmax选择指算子的概率计算\n",
    "def get_select_p(v):\n",
    "  exp_v = np.exp(v)\n",
    "  sum_exp_v = np.sum(exp_v)\n",
    "  return exp_v/sum_exp_v"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "### 进化函数\n",
    "def select_and_reproduction(population,num_range): \n",
    "\n",
    "  ### 按照指数选择算子有返回地选择父代中的个体\n",
    "  values = func(population[:,0],population[:,1])\n",
    "  select_p = get_select_p(values)\n",
    "  choices = np.random.choice(a=len(population), size=len(population), replace=True, p=select_p)\n",
    "  new_population = population[choices]\n",
    "\n",
    "  ### 交叉和变异\n",
    "  for i in range(int(len(new_population)/2)):\n",
    "    overlapping(new_population[2*i],new_population[2*i+1],p_c,dim) \n",
    "    variation(new_population[2*i],p_m,dim,num_range)\n",
    "    variation(new_population[2*i+1],p_m,dim,num_range)\n",
    "  \n",
    "  argmax_value = np.argmax(values)\n",
    "  ### 返回population中输出最大的值，对应的X，Y，和新的new_population\n",
    "  return values[argmax_value],population[argmax_value],new_population"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0 :   -25.9419025786\n",
      "1 :   -0.3959219757\n",
      "2 :   -0.0208101079129\n",
      "3 :   -0.0208101079129\n",
      "4 :   -0.0208101079129\n",
      "5 :   -0.0208101079129\n",
      "6 :   -0.0208101079129\n",
      "7 :   -0.0208101079129\n",
      "8 :   -0.0208101079129\n",
      "9 :   -0.0208101079129\n",
      "10 :   -0.0208101079129\n",
      "11 :   -0.0208101079129\n",
      "12 :   -0.0208101079129\n",
      "13 :   -0.0208101079129\n",
      "14 :   -0.0208101079129\n",
      "15 :   -0.0208101079129\n",
      "16 :   -0.0208101079129\n",
      "17 :   -0.0208101079129\n",
      "18 :   -0.0208101079129\n",
      "19 :   -0.0208101079129\n",
      "20 :   -0.0208101079129\n",
      "21 :   -0.0208101079129\n",
      "22 :   -0.0096627596887\n",
      "23 :   -0.0096627596887\n",
      "24 :   -0.0096627596887\n",
      "25 :   -0.0096627596887\n",
      "26 :   -0.0096627596887\n",
      "27 :   -0.0208101079129\n",
      "28 :   -0.0208101079129\n",
      "29 :   -0.0208101079129\n",
      "30 :   -0.0208101079129\n",
      "31 :   -0.0208101079129\n",
      "32 :   -0.0208101079129\n",
      "33 :   -0.0208101079129\n",
      "34 :   -0.0208101079129\n",
      "35 :   -0.00282360026574\n",
      "36 :   -0.0208101079129\n",
      "37 :   -0.0208101079129\n",
      "38 :   -0.0208101079129\n",
      "39 :   -0.0208101079129\n",
      "40 :   -0.0208101079129\n",
      "41 :   -0.0208101079129\n",
      "42 :   -0.0208101079129\n",
      "43 :   -0.0208101079129\n",
      "44 :   -0.0208101079129\n",
      "45 :   -0.0208101079129\n",
      "46 :   -0.0208101079129\n",
      "47 :   -0.0208101079129\n",
      "48 :   -0.0208101079129\n",
      "49 :   -0.0208101079129\n",
      "50 :   -0.0208101079129\n",
      "51 :   -0.0208101079129\n",
      "52 :   -0.0208101079129\n",
      "53 :   -0.000503804918331\n",
      "54 :   -0.000503804918331\n",
      "55 :   -0.0208101079129\n",
      "56 :   -0.0208101079129\n",
      "57 :   -0.0208101079129\n",
      "58 :   -0.0208101079129\n",
      "59 :   -0.0208101079129\n",
      "60 :   -0.0208101079129\n",
      "61 :   -0.0208101079129\n",
      "62 :   -0.0208101079129\n",
      "63 :   -0.0208101079129\n",
      "64 :   -0.0208101079129\n",
      "65 :   -0.0208101079129\n",
      "66 :   -0.0208101079129\n",
      "67 :   -0.0208101079129\n",
      "68 :   -0.0208101079129\n",
      "69 :   -0.0208101079129\n",
      "70 :   -0.0208101079129\n",
      "71 :   -0.0208101079129\n",
      "72 :   -0.0208101079129\n",
      "73 :   -0.0208101079129\n",
      "74 :   -0.0208101079129\n",
      "75 :   -0.0117021933432\n",
      "76 :   -0.0117021933432\n",
      "77 :   -0.0117021933432\n",
      "78 :   -0.0117021933432\n",
      "79 :   -0.0117021933432\n",
      "80 :   -0.000695132243068\n",
      "81 :   -0.000695132243068\n",
      "82 :   -0.0117021933432\n",
      "83 :   -0.0117021933432\n",
      "84 :   -0.0117021933432\n",
      "85 :   -0.0117021933432\n",
      "86 :   -0.0117021933432\n",
      "87 :   -0.000807412931352\n",
      "88 :   -0.000807412931352\n",
      "89 :   -0.000807412931352\n",
      "90 :   -0.000807412931352\n",
      "91 :   -0.0208101079129\n",
      "92 :   -0.0208101079129\n",
      "93 :   -0.0208101079129\n",
      "94 :   -0.0208101079129\n",
      "95 :   -0.0208101079129\n",
      "96 :   -0.0208101079129\n",
      "97 :   -0.0208101079129\n",
      "98 :   -0.0208101079129\n",
      "99 :   -0.00248424959647\n",
      "100 :   -0.00248424959647\n",
      "101 :   -0.0208101079129\n",
      "102 :   -0.0208101079129\n",
      "103 :   -0.0208101079129\n",
      "104 :   -0.0208101079129\n",
      "105 :   -0.0208101079129\n",
      "106 :   -0.0208101079129\n",
      "107 :   -0.0208101079129\n",
      "108 :   -0.0208101079129\n",
      "109 :   -0.0208101079129\n",
      "110 :   -0.0207319717866\n",
      "111 :   -0.0166192621868\n",
      "112 :   -0.00136880905496\n",
      "113 :   -0.00136880905496\n",
      "114 :   -0.00136880905496\n",
      "115 :   -0.00136880905496\n",
      "116 :   -0.00136880905496\n",
      "117 :   -0.00136880905496\n",
      "118 :   -0.00136880905496\n",
      "119 :   -0.00136880905496\n",
      "120 :   -0.00136880905496\n",
      "121 :   -0.00136880905496\n",
      "122 :   -0.00136880905496\n",
      "123 :   -0.00136880905496\n",
      "124 :   -0.00136880905496\n",
      "125 :   -0.00104290725877\n",
      "126 :   -0.00104290725877\n",
      "127 :   -0.00104290725877\n",
      "128 :   -0.00104290725877\n",
      "129 :   -0.00104290725877\n",
      "130 :   -0.00104290725877\n",
      "131 :   -0.00104290725877\n",
      "132 :   -0.0208101079129\n",
      "133 :   -0.0208101079129\n",
      "134 :   -0.0208101079129\n",
      "135 :   -0.0208101079129\n",
      "136 :   -0.0208101079129\n",
      "137 :   -0.0208101079129\n",
      "138 :   -0.0208101079129\n",
      "139 :   -0.000396879510416\n",
      "140 :   -0.000396879510416\n",
      "141 :   -0.0188165726359\n",
      "142 :   -0.0188165726359\n",
      "143 :   -0.0188165726359\n",
      "144 :   -0.0188165726359\n",
      "145 :   -0.0188165726359\n",
      "146 :   -0.0158082806612\n",
      "147 :   -0.0158082806612\n",
      "148 :   -0.0158082806612\n",
      "149 :   -0.0158082806612\n",
      "总共耗时： 495.85013484954834\n",
      "迭代最大值： -0.000396879510416\n",
      "对应坐标： [-2.98567253  3.01384207]\n"
     ]
    }
   ],
   "source": [
    "### 遗传函数主体\n",
    "### population存储每次迭代的解的集合\n",
    "### times_population存储所有的解的集合\n",
    "population = initialize(individual_n,num_range)\n",
    "times_population = np.empty([iterate_times,individual_n,dim])\n",
    "max_value = float('-inf')\n",
    "max_value_coordinate = None\n",
    "\n",
    "for i in range(iterate_times):\n",
    "  times_population[i] = population\n",
    "  last_max_value,last_max_value_pos,population = select_and_reproduction(population,num_range)\n",
    "  if last_max_value>max_value:\n",
    "    max_value = last_max_value\n",
    "    max_value_coordinate = last_max_value_pos\n",
    "  print(i,\":  \",last_max_value) \n",
    "  \n",
    "time_end=time.time()\n",
    "print('总共耗时：',time_end-time_start)\n",
    "print('迭代最大值：',max_value)\n",
    "print('对应坐标：',max_value_coordinate)\n",
    "######################################################################"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "######################################################################\n",
    "#\n",
    "# 绘图部分\n",
    "#\n",
    "######################################################################\n",
    "import matplotlib.pyplot as plt\n",
    "import matplotlib.animation as animation"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "# 定义画布\n",
    "fig, ax = plt.subplots()\n",
    "xdata, ydata = [], []\n",
    "ln, = plt.plot([], [],'ro',animated=True,color='black',markersize = 2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "# 初始化绘图画面 \n",
    "def init():\n",
    "  ax.set_xlim(num_range[0],num_range[1])\n",
    "  ax.set_ylim(num_range[0],num_range[1])\n",
    "  ax.set_xlabel('X')\n",
    "  ax.set_ylabel('Y')\n",
    "  return ln, "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "# 更新画面内容\n",
    "def update(frame):  \n",
    "  frame = int(frame) \n",
    "  xdata = times_population[frame,:,0]\n",
    "  ydata = times_population[frame,:,1]\n",
    "  ln.set_data(xdata, ydata) \n",
    "  return ln, "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAZUAAAEPCAYAAACKplkeAAAABHNCSVQICAgIfAhkiAAAAAlwSFlz\nAAALEgAACxIB0t1+/AAADXRJREFUeJzt3V2oZeV9x/HvT6ZSkhRr0ziCGivYmElpMV4MQijdkNSX\nXmRsaGWSiyipJdQKhRaaGaTMNASiQmxpiyk0Ym0xGL1INSUxo+gJ5KaOWGvs+DJQRp2JTnqRCqU3\no/57sZdme2bOcc6Z/5y19/H7gY17P/tlnvNw9nxda+21J1WFJEkdzhh7ApKkzcOoSJLaGBVJUhuj\nIklqY1Q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WqRFUVWvJ/lz4CHgUwZFm5G7v6SN9TvAj4FfH3si0ulgVKQNMnw55yeB\ny4E/TbL1XZ4iLRyjIm2cO4A/GQ7a38b0Hz2TNhWjIm2AJH8IvFhVjw5DXwc+muQ3R5yW1M5Pf0mS\n2rilIklqY1QkSW2MiiSpjVGRJLUxKpKkNkZFktTGqEiS2hgVSVKb/wfoMG5YBZHH+gAAAABJRU5E\nrkJggg==\n",
      "text/plain": [
       "<matplotlib.figure.Figure at 0x22ece04f4e0>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "# 动态绘图\n",
    "anim = animation.FuncAnimation(fig, update, frames=np.linspace(0,iterate_times-1,iterate_times),interval=100,\n",
    "                    init_func=init,blit=True)\n",
    "plt.show()\n",
    "######################################################################"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Empty",
   "language": "python",
   "name": "empty"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.5.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
